<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1" />
		<title>企业信息列表</title>
		<script type="text/javascript" src="js/common.js"></script>
	</head>
	<body>
		<div id="frame_child">
			<div class="accounttitle">
				<p>企业>企业列表</p>
            </div>

			<table class="table_at">
				<tr class="ac_title">
					<th style="width: 10%;">序号</th>
					<th style="width: 30%;">企业名称</th>
					<th style="width: 15%;">所在省份</th>
                    <th style="width: 15%;">注册资本</th>
                    <th style="width: 15%;">注册时间</th>
					<th style="width: 15%;">联系人操作</th>
                </tr>
                <tbody id="list">

                </tbody>
			</table>
			<!--分页-->
			<div class="pagination-box">
                <ul class="pagination">
                </ul>
            </div>
        </div>
        <!-- 增加模态框（Modal） -->
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">企业联系人</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form" id="myModal1-body">
                            <div class="form-group">
                                <label for="name1" class="col-sm-2 control-label">姓名</label>
                                <div class="col-sm-10">
                                  <input type="text" class="form-control" id="name1" placeholder="请输入联系人姓名（必填）">
                                </div>
                              </div>
                              <div class="form-group">
                                <label for="position1" class="col-sm-2 control-label">职位</label>
                                <div class="col-sm-10">
                                  <input type="text" class="form-control" id="position1" placeholder="请输入联系人职位（必填）">
                                </div>
                              </div>
                              <div class="form-group">
                                <label for="tel1" class="col-sm-2 control-label">联系方式</label>
                                <div class="col-sm-10">
                                  <input type="text" class="form-control" id="tel1" placeholder="请输入联系人手机/电话号码" onkeyup="value=value.replace(/[^\d\-\d]/g,'')" maxlength=20>
                                </div>
                              </div>
                        </form>                          
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary">增加联系人</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
        <!-- 修改模态框（Modal2） -->
        <div class="modal fade" id="myModal2" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">企业联系人</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form" id="myModal2-body">

                        </form>                          
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
        <!-- 删除模态框（Modal2） -->
        <div class="modal fade" id="myModal3" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                        <h4 class="modal-title" id="myModalLabel">企业联系人</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form" id="myModal3-body">

                        </form>                          
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div>
        <!-- /.modal -->
        <div class="modal fade" id="loadingModal" backdrop="static" keyboard="false">
            　　
            <div style="width: 300px;height:100px; z-index: 20000; position: absolute; text-align: center; left: 50%; top: 50%;margin-left:-100px;margin-top:-10px">
                　　　　
                <div class="progress progress-striped active"
                    style="margin-bottom: 0;height:50px; text-align:center;line-height: 50px;font-size:14px;">
                    数据加载中,请稍候......　　　　
                </div>
                　　
            </div>
        </div>
        <script type="text/javascript">
            var pageNo = 1
            var pageSize = 12
            var nowEntName = '' // 存储当前选中企业的名称
			
            $(document).ready(function(){
                loadResult()
                $('#searchBtn').click(function(){
                    loadResult()
                })
                
                // 增加按钮
                $('#list').on('click', '.do-add', function(){
                    nowEntName = $(this).attr('add-entName')
                    $('#name1').val('')
                    $('#position1').val('')
                    $('#tel1').val('')
                    $('#myModal1').modal('show');
                    return
                    $.ajax({
                        url: service_url + '/a/zhiku/zkContacts/findByEntname?entname='+ encodeURIComponent(entName) +'&token=' + $.cookie('AuthorizationAdmin'),
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        contentType: 'application/json',
                        dataType: 'jsonp',
                        jsonpCallback:"success_jsonpCallback",  
                        processData: false, // 告诉jQuery不要去处理发送的数据
                        success: function(data) {
                            console.log(data)
                            if (data.status === '00') {
                                
                            } else {
                                showErr("查询详情失败",)
                            }   
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest, textStatus, errorThrown)
                        }
                    })
                })
                $('#myModal1 .btn-primary').click(function() {
                    var name = $('#name1').val()
                    var position = $('#position1').val()
                    var tel = $('#tel1').val()
                    if (name === '' || position === '') {
                        showErr("姓名或职位不能为空",)
                    } else {
                        var paramStr = ''
                        if (tel === '') {
                            paramStr = 'entname='+encodeURIComponent(nowEntName)+'&name=' + encodeURIComponent(name) + '&position=' + encodeURIComponent(position) + '&token=' + $.cookie('AuthorizationAdmin')
                        } else {
                            paramStr = 'entname='+encodeURIComponent(nowEntName)+'&name=' + encodeURIComponent(name) + '&position=' + encodeURIComponent(position) +'&tel='+encodeURIComponent(tel)+ '&token=' + $.cookie('AuthorizationAdmin')
                        }
                        $.ajax({
                            url: service_url + '/a/zhiku/zkContacts/save?' + paramStr,
                            type: 'POST',
                            xhrFields: {
                                withCredentials: true
                            },
                            crossDomain: true,
                            contentType: 'application/json',
                            dataType: 'jsonp',
                            jsonpCallback:"success_jsonpCallback",  
                            processData: false, // 告诉jQuery不要去处理发送的数据
                            success: function(data) {
                                console.log(data)
                                if (data.status === '00') {
                                    top.alert("增加联系人成功",'','',{type: 'success'})
                                    $('#myModal1').modal('hide')
                                } else {
                                    showErr("增加联系人失败")
                                }   
                            },
                            error: function(XMLHttpRequest, textStatus, errorThrown) {
                                console.log(XMLHttpRequest, textStatus, errorThrown)
                            }
                        })
                    }
                })
            
                // 修改按钮
                $('#list').on('click', '.do-edit', function(){
                    nowEntName = $(this).attr('add-entName')
                    $('#myModal2-body').empty()
                    $.ajax({
                        url: service_url + '/a/zhiku/zkContacts/findByEntname?entname='+ encodeURIComponent(nowEntName) +'&token=' + $.cookie('AuthorizationAdmin'),
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        contentType: 'application/json',
                        dataType: 'jsonp',
                        jsonpCallback:"success_jsonpCallback",  
                        processData: false, // 告诉jQuery不要去处理发送的数据
                        success: function(data) {
                            console.log(data)
                            if (data.status === '00') {
                                var data = JSON.parse(data.data)
                                if (data.length > 0) {
                                    var tempHtml = ''
                                    for (var i = 0; i < data.length; i++) {
                                        var item = data[i]
                                        if (i === data.length - 1) {
                                            tempHtml = tempHtml + '<div class="form-group"><label class="col-sm-2 control-label">姓名</label><div class="col-sm-10"><input type="text" class="form-control" id="name'+item.id+'" placeholder="请输入联系人姓名（必填）" value="'+item.name+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">职位</label><div class="col-sm-10"><input type="text" class="form-control" id="position'+item.id+'" placeholder="请输入联系人职位（必填）" value="'+item.position+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">联系方式</label><div class="col-sm-10"><input type="text" class="form-control" id="tel'+item.id+'" placeholder="请输入联系人手机/电话号码"  value="'+(item.tel || '')+'" onkeyup="value=value.replace(/[^\d\-\d]/g,"")" maxlength=20></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="button" class="btn btn-default" add-id="'+item.id+'">保存修改</button></div></div>'
                                        } else {
                                            tempHtml = tempHtml + '<div class="form-group"><label class="col-sm-2 control-label">姓名</label><div class="col-sm-10"><input type="text" class="form-control" id="name'+item.id+'" placeholder="请输入联系人姓名（必填）" value="'+item.name+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">职位</label><div class="col-sm-10"><input type="text" class="form-control" id="position'+item.id+'" placeholder="请输入联系人职位（必填）" value="'+item.position+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">联系方式</label><div class="col-sm-10"><input type="text" class="form-control" id="tel'+item.id+'" placeholder="请输入联系人手机/电话号码" value="'+(item.tel || '')+'" onkeyup="value=value.replace(/[^\d\-\d]/g,"")" maxlength=20></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="button" class="btn btn-default" add-id="'+item.id+'">保存修改</button></div></div><hr>'
                                        }
                                    }
                                    $('#myModal2-body').append(tempHtml)
                                    $('#myModal2').modal('show');
                                    $('#myModal2 .btn-default').click(function() {
                                        var id = $(this).attr('add-id')
                                        var name = $('#name'+id).val()
                                        var position = $('#position'+id).val()
                                        var tel = $('#tel'+id).val()
                                        if (name === '' || position === '') {
                                            showErr("姓名或职位不能为空",)
                                        } else {
                                            var paramStr = ''
                                            if (tel === '') {
                                                paramStr = 'id='+id+'&entname='+encodeURIComponent(nowEntName)+'&name=' + encodeURIComponent(name) + '&position=' + encodeURIComponent(position) + '&token=' + $.cookie('AuthorizationAdmin')
                                            } else {
                                                paramStr = 'id='+id+'&entname='+encodeURIComponent(nowEntName)+'&name=' + encodeURIComponent(name) + '&position=' + encodeURIComponent(position) +'&tel='+encodeURIComponent(tel)+ '&token=' + $.cookie('AuthorizationAdmin')
                                            }
                                            $.ajax({
                                                url: service_url + '/a/zhiku/zkContacts/save?' + paramStr,
                                                type: 'POST',
                                                xhrFields: {
                                                    withCredentials: true
                                                },
                                                crossDomain: true,
                                                contentType: 'application/json',
                                                dataType: 'jsonp',
                                                jsonpCallback:"success_jsonpCallback",  
                                                processData: false, // 告诉jQuery不要去处理发送的数据
                                                success: function(data) {
                                                    console.log(data)
                                                    if (data.status === '00') {
                                                        top.alert("修改联系人信息成功",'','',{type: 'success'})
                                                    } else {
                                                        showErr("修改联系人信息失败")
                                                    }   
                                                },
                                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                                    console.log(XMLHttpRequest, textStatus, errorThrown)
                                                }
                                            })
                                        }

                                    })
                                } else {
                                    showErr("企业暂无联系人信息")
                                }
                            } else {
                                showErr("获取联系人列表失败")
                            }   
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest, textStatus, errorThrown)
                        }
                    })
                })
                
             // 删除按钮
             $('#list').on('click', '.do-delete', function(){
                    nowEntName = $(this).attr('add-entName')
                    $('#myModal3-body').empty()
                    $.ajax({
                        url: service_url + '/a/zhiku/zkContacts/findByEntname?entname='+ encodeURIComponent(nowEntName) +'&token=' + $.cookie('AuthorizationAdmin'),
                        type: 'POST',
                        xhrFields: {
                            withCredentials: true
                        },
                        crossDomain: true,
                        contentType: 'application/json',
                        dataType: 'jsonp',
                        jsonpCallback:"success_jsonpCallback",  
                        processData: false, // 告诉jQuery不要去处理发送的数据
                        success: function(data) {
                            console.log(data)
                            if (data.status === '00') {
                                var data = JSON.parse(data.data)
                                if (data.length > 0) {
                                    var tempHtml = ''
                                    for (var i = 0; i < data.length; i++) {
                                        var item = data[i]
                                        if (i === data.length - 1) {
                                            tempHtml = tempHtml + '<div class="con-item"><div class="form-group"><label class="col-sm-2 control-label">姓名</label><div class="col-sm-10"><input type="text" class="form-control" id="name'+item.id+'" placeholder="请输入联系人姓名（必填）" value="'+item.name+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">职位</label><div class="col-sm-10"><input type="text" class="form-control" id="position'+item.id+'" placeholder="请输入联系人职位（必填）" value="'+item.position+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">联系方式</label><div class="col-sm-10"><input type="text" class="form-control" id="tel'+item.id+'" placeholder="请输入联系人手机/电话号码"  value="'+(item.tel || '')+'" onkeyup="value=value.replace(/[^\d\-\d]/g,"")" maxlength=20></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="button" class="btn btn-default" add-id="'+item.id+'">删除</button></div></div></div>'
                                        } else {
                                            tempHtml = tempHtml + '<div class="con-item"><div class="form-group"><label class="col-sm-2 control-label">姓名</label><div class="col-sm-10"><input type="text" class="form-control" id="name'+item.id+'" placeholder="请输入联系人姓名（必填）" value="'+item.name+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">职位</label><div class="col-sm-10"><input type="text" class="form-control" id="position'+item.id+'" placeholder="请输入联系人职位（必填）" value="'+item.position+'"></div></div><div class="form-group"><label class="col-sm-2 control-label">联系方式</label><div class="col-sm-10"><input type="text" class="form-control" id="tel'+item.id+'" placeholder="请输入联系人手机/电话号码" value="'+(item.tel || '')+'" onkeyup="value=value.replace(/[^\d\-\d]/g,"")" maxlength=20></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="button" class="btn btn-default" add-id="'+item.id+'">删除</button></div></div><hr></div>'
                                        }
                                    }
                                    $('#myModal3-body').append(tempHtml)
                                    $('#myModal3').modal('show');
                                    $('#myModal3 .btn-default').click(function() {
                                        var id = $(this).attr('add-id')
                                        var that = this
                                        $.ajax({
                                                url: service_url + '/a/zhiku/zkContacts/deleteContacts/' + id + '?token=' + $.cookie('AuthorizationAdmin'),
                                                type: 'POST',
                                                xhrFields: {
                                                    withCredentials: true
                                                },
                                                crossDomain: true,
                                                contentType: 'application/json',
                                                dataType: 'jsonp',
                                                jsonpCallback:"success_jsonpCallback",  
                                                processData: false, // 告诉jQuery不要去处理发送的数据
                                                success: function(data) {
                                                    if (data.status === '00') {
                                                        top.alert("删除联系人信息成功",'','',{type: 'success'})
                                                        $(that).parents('.con-item').remove()
                                                    } else {
                                                        showErr("删除联系人信息失败")
                                                    }   
                                                },
                                                error: function(XMLHttpRequest, textStatus, errorThrown) {
                                                    console.log(XMLHttpRequest, textStatus, errorThrown)
                                                }
                                            })
                                    })
                                } else {
                                    showErr("企业暂无联系人信息")
                                }
                            } else {
                                showErr("获取联系人列表失败")
                            }   
                        },
                        error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.log(XMLHttpRequest, textStatus, errorThrown)
                        }
                    })
                })
               
                  
            })
            // 根据条件查询
            function loadResult() {
                showLoading()
                $('#list').empty()
                var paramStr = 'pageNo='+pageNo+'&pageSize='+pageSize+'&token=' + $.cookie('AuthorizationAdmin')
                $.ajax({
                    url: service_url + '/a/zhiku/zkEntInfo/advancedSerach?'+paramStr,
                    type: 'POST',
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    contentType: 'application/json',
                    dataType: 'jsonp',
                    jsonpCallback:"success_jsonpCallback",  
                    processData: false, // 告诉jQuery不要去处理发送的数据
                    success: function(data) {
                        hideLoading()
                        var list = data.data
                        if (data.count === 0) {
                            $('#list').append('<tr><td colspan="6">暂无数据</td></tr>')
                        } else {
                            var tempHtml = ''
                            for (var i = 0; i < list.length; i++) {
                                var regcap = list[i].regcap || 0
                                var regcapStr = regcap / 10000 >= 1 ? (regcap / 10000) + '亿元' : regcap + '万元'
                                var regorgProvince = list[i].regorgProvince || '未知'
                                var esdateStr = list[i].esdateStr  || '未知'
                                var doHtml = '<span class="do-add" add-entName="'+list[i].entName+'">增加</span><span class="do-edit" add-entName="'+list[i].entName+'">修改</span><span class="do-delete" add-entName="'+list[i].entName+'">删除</span>'

                                tempHtml = tempHtml + '<tr><td>'+((pageNo-1)*pageSize+i+1)+'</td><td>'+list[i].entName+'</td><td>'+regorgProvince+'</td><td>'+regcapStr+'</td><td>'+esdateStr+'</td><td>'+doHtml+'</td></tr>'
                            }
                            $('#list').append(tempHtml)
                            $('.pagination-box').show()
                            $('.pagination').jqPaginator({
                                totalCounts: data.count,
                                pageSize: pageSize,
                                visiblePages: 10,
                                currentPage: pageNo,
                                first: '<li class="first"><a href="javascript:void(0);">首页<\/a><\/li>',
                                prev: '<li class="prev"><a href="javascript:void(0);">&laquo;<\/a><\/li>',
                                next: '<li class="next"><a href="javascript:void(0);">&raquo;<\/a><\/li>',
                                last: '<li class="last"><a href="javascript:void(0);">末页<\/a><\/li>',
                                page: '<li class="page"><a href="javascript:void(0);">{{page}}<\/a><\/li>',
                                onPageChange: function (num, type) {
                                    if (type === 'change') {
                                        pageNo = num
                                        loadResult()
                                    }
                                }
                            });
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        hideLoading()
                        console.log(XMLHttpRequest, textStatus, errorThrown)
                    }
                })
            }

        </script>
	</body>

</html>